package com.muse.openfeign.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.muse.openfeign.dto.User;
import com.muse.openfeign.feign.server.ProviderService;

import lombok.extern.slf4j.Slf4j;

/**
 * @description
 * @author: muse
 * @create: 2021-08-08 21:39
 **/
@Slf4j
@RequestMapping("/consumer")
@RestController
public class ConsumerController {

    @Resource
    private ProviderService providerService;

    // http://localhost:8002/consumer/hello
    @GetMapping("/hello")
    public String hello() {
        log.info("Feign invoke!");
        return providerService.hello();
    }

    /**
     * 传递基本类型参数
     * http://localhost:8002/consumer/baseParam?name=muse&age=20
     */
    @GetMapping("/baseParam")
    public String baseParam(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        log.info("consumer baseParam name={} age={}", name, age);
        return providerService.baseParam(name, age);
    }

    /**
     * 传递基本类型参数——路径
     * http://localhost:8002/consumer/baseParamPath/bob/30
     */
    @GetMapping("/baseParamPath/{name}/{age}")
    public String baseParamPath(@PathVariable("name") String name, @PathVariable("age") Integer age) {
        log.info("consumer baseParamPath name={} age={}", name, age);
        return providerService.baseParamPath(name, age);
    }

    /**
     * 传递对象类型参数
     * http://localhost:8002/consumer/objectParam
     */
    @GetMapping("/objectParam")
    public String objectParam() {
        User user = new User();
        user.setName("John");
        user.setAge(40);
        log.info("consumer objectParam name={} age={}", user.getName(), user.getAge());
        return providerService.objectParam(user);
    }

    /**
     * 传递数组类型参数
     * http://localhost:8002/consumer/arrayParam
     */
    @GetMapping("/arrayParam")
    public String arrayParam() {
        log.info("consumer arrayParam");
        String[] params = new String[]{"muse", "bob", "john"};
        return providerService.arrayParam(params);
    }

    /**
     * 传递集合类型参数
     * http://localhost:8002/consumer/listParam
     */
    @GetMapping("/listParam")
    public String listParam() {
        log.info("consumer listParam");
        List params = Lists.newArrayList("zhang3", "lee4", "wang5");
        return providerService.listParam(params);
    }

    /**
     * 传递集合类型参数
     * http://localhost:8002/consumer/responseObj?userId=111
     */
    @GetMapping("/responseObj")
    public User responseObj(@RequestParam("userId") String userId) {
        User user = providerService.responseObj(userId);
        log.info("consumer responseObj userId={} user={}", userId, user);
        return user;
    }

    /**
     * 测试OpenFeign默认1秒请求超时
     * http://localhost:8002/consumer/timeoutDemo
     */
    @GetMapping("/timeoutDemo")
    public String timeoutDemo() {
        log.info("consumer timeoutDemo!");
        providerService.timeoutDemo();
        return "consumer timeoutDemo!";
    }

}
